package com.whxd.system.user.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.whxd.system.authority.entity.Authority;
import com.whxd.system.user.entity.User;

public interface UserDao extends PagingAndSortingRepository<User, Long> {

	public User findByUsername(String username);

	// public static User findByUsername(String username) {
	// try {
	// User user = (User)
	// em().createQuery("select u from User u where u.username=:username")
	// .setParameter("username", username).getSingleResult();
	// return user;
	// } catch (NoResultException ne) {
	// return null;
	// }
	// }
	//
	// public static PageResult<User> findByCondition(PageParam page,
	// UserCondition userCondition) {
	// // 拼装where
	// Map<String, Object> params = new HashMap<String, Object>();
	// String whereJpql = "";
	// if (userCondition != null) {
	// whereJpql += " where 1=1 ";
	// if (StringUtils.isNotBlank(userCondition.getUsername())) {
	// whereJpql += " and u.username like :username";
	// params.put("username", "%" + userCondition.getUsername() + "%");
	// }
	// if (StringUtils.isNotBlank(userCondition.getFullname())) {
	// whereJpql += " and u.fullname like :fullname";
	// params.put("fullname", "%" + userCondition.getFullname() + "%");
	// }
	// if (userCondition.getCreatedTimeStart() != null) {
	// whereJpql += " and u.createdTime >= :createdTimeStart";
	// params.put("createdTimeStart", userCondition.getCreatedTimeStart());
	// }
	// if (userCondition.getCreatedTimeEnd() != null) {
	// whereJpql += " and u.createdTime <= :createdTimeEnd";
	// params.put("createdTimeEnd", userCondition.getCreatedTimeEnd());
	// }
	// if (userCondition.getEnabled() != null) {
	// whereJpql += " and u.enabled = :enabled";
	// params.put("enabled", userCondition.getEnabled());
	// }
	// }
	// // 拼装order
	// String orderJpql = "";
	// if (StringUtils.isNotBlank(page.getSort()) &&
	// StringUtils.isNotBlank(page.getOrder())) {
	// orderJpql += " order by u." + page.getSort() + " " + page.getOrder();
	// }
	// // 创建查询
	// Query query = em().createQuery("select u from User u " + whereJpql +
	// orderJpql);
	// Query queryCount = em().createQuery("select count(u) from User u " +
	// whereJpql);
	// // 填充where参数
	// if (params != null && !params.isEmpty()) {
	// for (String key : params.keySet()) {
	// query.setParameter(key, params.get(key));
	// queryCount.setParameter(key, params.get(key));
	// }
	// }
	// // 填充分页信息
	// query.setFirstResult(page.getFirstResult()).setMaxResults(page.getRows());
	// // 返回结果
	// PageResult<User> result = new PageResult<User>();
	// result.setTotal((Long) queryCount.getSingleResult());
	// result.setRows(query.getResultList());
	// return result;
	// }
	//
	// /**
	// * 根据搜索选项查询用户选择页面的数据
	// *
	// * @author 张世鹏
	// * @since 2013-8-16
	// * @param page
	// * @param condition
	// * @return
	// */
	// public static PageResult<User> findByUserSelectCondition(PageParam page,
	// UserSelectCondition condition) {
	// // 拼装where
	// Map<String, Object> params = new HashMap<String, Object>();
	// String whereJpql = "";
	// if (condition != null) {
	// whereJpql += " where 1=1 ";
	// if (StringUtils.isNotBlank(condition.getUsername())) {
	// whereJpql += " and u.username like :username";
	// params.put("username", "%" + condition.getUsername() + "%");
	// }
	// if (StringUtils.isNotBlank(condition.getFullname())) {
	// whereJpql += " and u.fullname like :fullname";
	// params.put("fullname", "%" + condition.getFullname() + "%");
	// }
	// if (StringUtils.isNotBlank(condition.getOrgIds()) &&
	// !condition.getOrgIds().contains("-1")) {
	// whereJpql += " and u.organization.id in (" + condition.getOrgIds() + ")";
	// }
	// }
	// // 拼装order
	// String orderJpql = "";
	// if (StringUtils.isNotBlank(page.getSort()) &&
	// StringUtils.isNotBlank(page.getOrder())) {
	// orderJpql += " order by u." + page.getSort() + " " + page.getOrder();
	// }
	// // 创建查询
	// Query query = em().createQuery("select u from User u " + whereJpql +
	// orderJpql);
	// Query queryCount = em().createQuery("select count(u) from User u " +
	// whereJpql);
	// // 填充where参数
	// if (params != null && !params.isEmpty()) {
	// for (String key : params.keySet()) {
	// query.setParameter(key, params.get(key));
	// queryCount.setParameter(key, params.get(key));
	// }
	// }
	// // 填充分页信息
	// query.setFirstResult(page.getFirstResult()).setMaxResults(page.getRows());
	// // 返回结果
	// PageResult<User> result = new PageResult<User>();
	// result.setTotal((Long) queryCount.getSingleResult());
	// result.setRows(query.getResultList());
	// return result;
	// }
	//
	/**
	 * 查询用户的权限列表
	 */
	@Query("select distinct a.name from User u JOIN u.roles r JOIN r.ahthorities a where u.username=:username")
	public List<String> findAuthorityNameByUsername(@Param("username") String username);

	/**
	 * 根据用户名查询所有权限
	 */
	@Query("select a from User u JOIN u.roles r JOIN r.ahthorities a where u.username=?1")
	public List<Authority> findAuthorityByUsername(String username);
	//
	// /**
	// * 查询拥有某权限的用户
	// */
	// public static List<User> findUserByAuthorityName(String authorityName) {
	// Query query = em().createQuery(
	// "select distinct u from User u JOIN u.roles r JOIN r.ahthoritys a where a.name = :authorityName");
	// query.setParameter("authorityName", authorityName);
	// return query.getResultList();
	// }
	//
	// /**
	// * 获得用户是否拥有指定权限
	// */
	// public static boolean hasAuthority(Long userId, String authority) {
	// Query query = em()
	// .createQuery(
	// "select count(u.id) from User u JOIN u.roles r JOIN r.ahthoritys a where u.id = :userId and a.name = :authority");
	// query.setParameter("userId", userId);
	// query.setParameter("authority", authority);
	// return ((Long) query.getSingleResult()) > 0;
	// }
	//
	// /**
	// * 查询用户的菜单列表
	// *
	// * @author 张世鹏
	// * @since 2013-6-20
	// * @param username2
	// * @return
	// */
	// public static List<Menu> findMenuByUsername(String username) {
	// Query query = em()
	// .createQuery(
	// "select distinct m from User u JOIN u.roles r JOIN r.menus m where u.username=:username and m.enabled=true order by m.order asc");
	// query.setParameter("username", username);
	// return query.getResultList();
	// }
}
